package com.awspaas.user.apps.cpq.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.BOQueryAPI;
import com.alibaba.fastjson.JSONObject;

public class CreateMK {

	public void createMKSChange(BOQueryAPI params, BOQueryAPI modules, UserContext userContext, String pid,String bindId,String wfno) {
		try {
			
			List<BO> mlists=	modules.list();
			Map<String, JSONObject> pMap = new HashMap<String, JSONObject>();
			Map<String, String> nameMap = new HashMap<String, String>();
			Map<String, String> noMap = new HashMap<>();
			// 获取模块编码信息
			Map<String, BO> idMap = new HashMap<>();
			String bms = "";
			for (BO json : mlists) {
				//JSONObject json = (JSONObject) string;
				String bm = json.getString("MKBH");
				if (bms.isEmpty()) {
					bms = "'" + bm + "'";
				} else {
					bms += ",'" + bm + "'";
				}
				String id = json.getString("ID");
				idMap.put(id, json);
			}
			String sql = "select  * from  BO_EU_CPQ_MODULE_RECORD where  GLPID=? and MKBM in (" + bms + ")";
			List<RowMap> maps = DBSql.getMaps(sql,new Object[] {bindId});
			
			String wlsql ="select  a.MKBM,b.BINDID,b.XH,b.WLMC,b.WLBM,b.LX,b.GG,b.DH,b.DW,b.SL,b.DJ,b.WLJSGS,b.CLF,b.RGF,b.WXWG,b.CGF,b.ZJ,b.ZCLF,b.ZGSF,b.ZWX,b.ZCGF"
					+ " from BO_EU_CPQ_MODULE_RECORD_GRID  b left JOIN   BO_EU_CPQ_MODULE_RECORD a on b.BINDID=a.BINDID "
					+ "where a.GLPID=? and a.MKBM in (" + bms + ")";
			String zfsql = "select  a.MKBM,b.* from BO_EU_CPQ_MODULE_RECORD_STRAIGHT_HAIR   b left JOIN BO_EU_CPQ_MODULE_RECORD a on b.BINDID=a.BINDID "
					+ "where  a.GLPID=?  and a.MKBM in (" + bms + ")";
			String cssql = "select  a.MKBM,b.* from BO_EU_CPQ_MODULE_RECORD_PARAMETERS   b left JOIN BO_EU_CPQ_MODULE_RECORD a on b.BINDID=a.BINDID "
					+ "where a.GLPID=? and a.MKBM in (" + bms + ")";
			List<RowMap> wlmaps = DBSql.getMaps(wlsql,new Object[] {bindId});
			Map<String,List<RowMap>> wlmoduleMap = new HashMap<>();
			if (wlmaps.size() > 0) {
				for (RowMap rowMap : wlmaps) {
					String mkbm = rowMap.getString("MKBM");
					if(wlmoduleMap.containsKey(mkbm)) {
						List<RowMap> list=wlmoduleMap.get(mkbm);
						list.add(rowMap);
					}else {
						List<RowMap> list=new ArrayList<RowMap>();
						list.add(rowMap);
						wlmoduleMap.put(mkbm, list);
					}
					
				}
			}
			List<RowMap> zfmaps = DBSql.getMaps(zfsql,new Object[] {bindId});
			Map<String, List<RowMap>> zfmoduleMap = new HashMap<>();
			if (zfmaps.size() > 0) {
				for (RowMap rowMap : zfmaps) {
					String mkbm = rowMap.getString("MKBM");
					if(zfmoduleMap.containsKey(mkbm)) {
						List<RowMap> list=zfmoduleMap.get(mkbm);
						list.add(rowMap);
						zfmoduleMap.put(mkbm, list);
					}else {
						List<RowMap> list=new ArrayList<RowMap>();
						list.add(rowMap);
						zfmoduleMap.put(mkbm, list);
					}
				}
			}
			List<RowMap> csmaps = DBSql.getMaps(cssql,new Object[] {bindId});
			Map<String, List<RowMap>> csmoduleMap = new HashMap<>();
			if (csmaps.size() > 0) {
				for (RowMap rowMap : csmaps) {
					String mkbm = rowMap.getString("MKBM");
					if(csmoduleMap.containsKey(mkbm)) {
						List<RowMap> list=csmoduleMap.get(mkbm);
						list.add(rowMap);
						csmoduleMap.put(mkbm, list);
					}else {
						List<RowMap> list=new ArrayList<RowMap>();
						list.add(rowMap);
						csmoduleMap.put(mkbm, list);
					}
				}
			}
			if (maps.size() > 0) {
				Map<String, BO> mkMaps=new HashMap<>();
				Map<String, List<BO>> wlMaps=new HashMap<>();
				Map<String, List<BO>> zfMaps=new HashMap<>();
				Map<String, List<BO>> csMaps=new HashMap<>();
				for (RowMap rowMap : maps) {
					BO mkBo=new BO();
					String bm=rowMap.getString("MKBM");
					mkBo.set("SSXT", rowMap.getString("SSXT"));
					mkBo.set("PMKSSDL", rowMap.getString("PMKSSDL"));
					mkBo.set("MKMC", rowMap.getString("MKMC"));
					mkBo.set("MKBM", rowMap.getString("MKBM"));
					mkBo.set("GG", rowMap.getString("GG"));
					mkBo.set("DW", rowMap.getString("DW"));
					mkBo.set("CLF", rowMap.getString("CLF"));
					mkBo.set("RGF", rowMap.getString("RGF"));
					mkBo.set("WXWG", rowMap.getString("WXWG"));
					mkBo.set("CGF", rowMap.getString("CGF"));
					mkBo.set("ZFWZYF", rowMap.getString("ZFWZYF"));
					mkBo.set("MKDJ", rowMap.getString("DJ"));
					mkBo.set("ZZFBL", rowMap.getString("ZZFBL"));
					mkBo.set("ZZF",rowMap.getString("ZZFBL") );
					mkBo.set("DJZZF",rowMap.getString("ZZFBL"));
					mkBo.set("SITRUE", rowMap.getString("SITRUE"));
					mkBo.set("SLJSGZ", rowMap.getString("SLJSGZ"));
					mkBo.set("MKSL", rowMap.getString("MKSL"));
					mkBo.set("GLPID", pid);
					mkBo.set("XGBJDH", wfno);
					mkMaps.put(rowMap.getString("MKBM"), mkBo);
					//创建物料
					List<BO> lists=new ArrayList<>();
					if(wlmoduleMap.containsKey(bm)) {
						List<RowMap> materials=wlmoduleMap.get(bm);
						if(materials.size()>0) {
							for (RowMap bo : materials) {
								BO wlgrid=new BO();
								wlgrid.set("XH",bo.getString("XH") );
								wlgrid.set("WLMC", bo.getString("NAME"));
								wlgrid.set("WLBM", bo.getString("BM"));
								wlgrid.set("LX", bo.getString("TYPE"));
								wlgrid.set("GG", bo.getString("GG"));
								wlgrid.set("DH", bo.getString("TH"));
								wlgrid.set("DW", bo.getString("DW"));
								wlgrid.set("DJ", bo.getString("DJ"));
								wlgrid.set("CLF", bo.getString("CLF"));
								wlgrid.set("RGF",bo.getString("RGF") );
								wlgrid.set("WXWG",bo.getString("WXWG") );
								wlgrid.set("CGF",bo.getString("CGF") );
								wlgrid.set("SL",bo.getString("SL") );
								wlgrid.set("ZJ",bo.getString("ZJ") );
								wlgrid.set("ZCLF",bo.getString("ZCLF") );
								wlgrid.set("ZGSF",bo.getString("ZGSF") );
								wlgrid.set("ZWX",bo.getString("ZWX") );
								wlgrid.set("ZCGF",bo.getString("ZCGF") );
								wlgrid.set("WLJSGS",bo.getString("WLJSGS") );
								  lists.add(wlgrid); 
							}
							
						}
					}
					//对直发物资进行计算
					List<BO> zflist=new ArrayList<>();
					if(zfmoduleMap.containsKey(bm)) {
						List<RowMap> list=zfmoduleMap.get(bm);
						if(list.size()>0) {
							for (RowMap rowMap2 : list) {
								BO zfgrid=new BO();
								 zfgrid.set("MC", rowMap2.getString("MC"));
								 zfgrid.set("ZL", rowMap2.getString("ZL"));
								 zfgrid.set("DW", rowMap2.getString("DW"));
								 zfgrid.set("YFBZ", rowMap2.getString("YFBZ"));
								 zfgrid.set("MDSF", rowMap2.getString("MDSF"));
								 zfgrid.set("JSGS",rowMap2.getString("JSGS") );						   
								 zfgrid.set("BZ", rowMap2.getString("BZ"));
								 zfgrid.set("BM", rowMap2.getString("BM"));
								 zfgrid.set("YFJG",rowMap2.getString("YFJG"));
								 zfgrid.set("SL",rowMap2.getString("SL"));
								 zflist.add(zfgrid);
							}
						}
					}
					//对参数数据进行创建
					List<BO> cslists=new ArrayList<>();
					if(csmoduleMap.containsKey(bm)) {
						List<RowMap> list=csmoduleMap.get(bm);
						if(list.size()>0) {
							for (RowMap rowMap2 : list) {
								BO cs=new BO();
								String no=rowMap2.getString("PARMENTIER_NO");								
								cs.set("PARMENTIER_NAME", rowMap2.getString("PARMENTIER_NAME"));
								cs.set("PARMENTIER_NO", no);
								cs.set("PARMENTIER_VALUE",rowMap2.getString("PARMENTIER_VALUE"));
								cs.set("DW", rowMap2.getString("DW"));
								cs.set("BZ", rowMap2.getString("BZ"));
								cs.set("XH", rowMap2.getString("XH"));
								cslists.add(cs);
							}
						}
					}
					wlMaps.put(bm, lists);
					zfMaps.put(bm, zflist);
					csMaps.put(bm, cslists);
				}
				//创建对于的物料清单信息
				if(mkMaps.size()>0) {
					for (String bm : mkMaps.keySet()) {
						BO data=mkMaps.get(bm);
						ProcessInstance mpid=SDK.getProcessAPI().createBOProcessInstance("obj_338e4eb2168e4219811be2ab72f0e255", userContext.getUID(), "");
						SDK.getBOAPI().create("BO_EU_CPQ_MODULE_RECORD", data, mpid.getId(), userContext.getUID());
                        if(wlMaps.containsKey(bm)) {
                        	List<BO> list=wlMaps.get(bm);
                        	if(list.size()>0) {
                        		SDK.getBOAPI().create("BO_EU_CPQ_MODULE_RECORD_GRID", list, mpid.getId(), userContext.getUID());
                        	}
                        }
                        if(zfMaps.containsKey(bm)) {
                        	List<BO> list=zfMaps.get(bm);
                        	if(list.size()>0) {
                        		SDK.getBOAPI().create("BO_EU_CPQ_MODULE_RECORD_STRAIGHT_HAIR", list, mpid.getId(), userContext.getUID());
                        	}
                        }
                        if(csMaps.containsKey(bm)) {
                        	List<BO> list=csMaps.get(bm);
                        	if(list.size()>0) {
                        		SDK.getBOAPI().create("BO_EU_CPQ_MODULE_RECORD_PARAMETERS", list, mpid.getId(), userContext.getUID());
                        	}
                        }
					}
				}

				
			}
			
		} catch (Exception e) {
			throw e;
		}
		
	}

}
